package com.margo.project.redis.lock.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.margo.project.redis.lock.service.RedisLockService;



/**
 * 
 * @author fxb208
 *
 */
@RestController
public class RedisLockController {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private RedisLockService redisLockService;
	
	/**
	 * localhost:8080/redis/lock/margo
	 * @param projectId
	 * @return
	 * @throws InterruptedException
	 */
	@RequestMapping(path="/redis/lock/{projectId}")
	private String insertDB(@PathVariable String projectId) throws InterruptedException {
		return redisLockService.insertDB(projectId);
	}
	
	@RequestMapping(path="/redis/ops/{projectId}")
	private String redisOps(@PathVariable String projectId) throws InterruptedException {
		stringRedisTemplate.opsForValue().setIfAbsent(projectId, "lock");
		return "hello world";
	}
}
